/*
Authors: Eli Yucht 300388949 and Lior Fass 300069705
Project: SCP (Switch Schedule Policy)
Description: The main function
*/


#include "RR_scheduler.h"
#include "WRR_Scheduler.h"
#include "DRR_Scheduler.h"
#define ARGS_NUM 4

/* Check the validation of the arguments:
	1. num of arguments = 4
	2. scheduler type have to be "RR" or "WRR or "DRR"
	3. quantum is Non-negative

	If all the above checks are valid 
	a new scheduler object (with type according to argv[1]) is return to *scheduler 
	and the function return 0, otherwise return 1
	*/
int Check_Args(int argc, char * argv[], Scheduler** scheduler);

/*The main function does the following tasks:
	1. Check arguments validation
	2. Initialize the Scheduler
	3. Run the appropriate scheduler
	
	If all the tasks return without errors we return 0, otherwise 1 is return*/
int main(int argc, char * argv[])
{
	Scheduler* scheduler = NULL;
	int status;

	status = Check_Args(argc,argv,&scheduler);
	if (status != 0)
	{
		return 1;
	}

	status = scheduler->Initialize_Scheduler(atoi(argv[2]), argv[3], argv[4]);
	if (status != 0)
	{
		delete scheduler;
		return 1;
	}

	status = scheduler->RunScheduler();
	delete scheduler;
	return status;
	
}


int Check_Args(int argc, char * argv[], Scheduler** scheduler)
{
	if (argc != ARGS_NUM + 1)
	{
		cerr << "Invalid num of arguments for sch.exe.\n";
		return 1;
	}

	if (strcmp(argv[1], "RR") == 0)
		*scheduler = new RR_Scheduler();
	else if (strcmp(argv[1], "WRR") == 0)
		*scheduler = new WRR_Scheduler();
	else if (strcmp(argv[1], "DRR") == 0)
		*scheduler = new DRR_Scheduler();
	else
	{
		cerr << "Invalid scheduler type.\n" <<
			"Only \"RR\" or \"WRR\" or \"DRR\" allowed.\n";
		return 1;
	}

	if (atoi(argv[2]) < 0)
	{
		delete *scheduler;
		cerr << "Invalid Quantum.\n";
		return 1;
	}
	return 0;
}